package cn.itcast.web.controller;



import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.UserService;
import com.alibaba.excel.util.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


//@Controller
public class LoginController extends BaseController{

    @Autowired
    private UserService userService;

    @Autowired
    private ModuleService moduleService;


    @RequestMapping("/login")
    public String login(String email,String password) {
        //1、判断邮箱和密码是否为空
        if(StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            return "forward:/login.jsp";
        }
        try {
            //2、获取subject工具类
            Subject subject = SecurityUtils.getSubject();
            //3、调用subject的login方法进入shiro登录
            UsernamePasswordToken upToken = new UsernamePasswordToken(email,password); //登录参数
            subject.login(upToken);
            //4、如果正常执行无异常，登录成功
            //从shiro中获取安全数据（登录用户）
            User user = (User) subject.getPrincipal();
            //将用户对象和模块列表对象存入session
            session.setAttribute("loginUser",user);
            List<Module> list = moduleService.findByUser(user);
            session.setAttribute("modules",list);
            //跳转到主页
            return "home/main";
        }catch (Exception e) {
            e.printStackTrace();
            //5、如果抛出异常，登录失败（用户名或者密码错误）
            request.setAttribute("error", "用户名或者密码错误");
            return "forward:/login.jsp";
        }
    }

    //退出
    @RequestMapping(value = "/logout",name="用户登出")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout(); //shiro的用户退出登录
        return "forward:login.jsp";
    }

    @RequestMapping("/home")
    public String home(){
	    return "home/home";
    }
}
